python writelines换行

您所在的位置:网站首页 python writelines 换行 python writelines换行

python writelines换行

2024-02-04 13:48| 来源: 网络整理| 查看: 265

我有一个程序可以将列表写入文件。

列表是由管道分隔的行组成的列表,这些行应按如下方式写入文件:123|GSV|Weather_Mean|hello|joe|43.45

122|GEV|temp_Mean|hello|joe|23.45

124|GSI|Weather_Mean|hello|Mike|47.45

但上面写着这句话啊:123|GSV|Weather_Mean|hello|joe|43.45122|GEV|temp_Mean|hello|joe|23.45124|GSI|Weather_Mean|hello|Mike|47.45

这个程序把所有的行写成一行,没有换行符。。这让我很伤心,我得想办法扭转这种局面,但无论如何,我的程序哪里出错了?我认为写行应该写下文件的行数,而不是只写一行。。fr = open(sys.argv[1], 'r') # source file

fw = open(sys.argv[2]+"/masked_"+sys.argv[1], 'w') # Target Directory Location

for line in fr:

line = line.strip()

if line == "":

continue

columns = line.strip().split('|')

if columns[0].find("@") > 1:

looking_for = columns[0] # this is what we need to search

else:

looking_for = "[email protected]"

if looking_for in d:

# by default, iterating over a dictionary will return keys

new_line = d[looking_for]+'|'+'|'.join(columns[1:])

line_list.append(new_line)

else:

new_idx = str(len(d)+1)

d[looking_for] = new_idx

kv = open(sys.argv[3], 'a')

kv.write(looking_for+" "+new_idx+'\n')

kv.close()

new_line = d[looking_for]+'|'+'|'.join(columns[1:])

line_list.append(new_line)

fw.writelines(line_list)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3